<?php


namespace Soen\Array_Package\PackageProvider;


use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Scalar\String_;
use Soen\Array_Package\Pack\ArrayItemsPack;
use Soen\Array_Package\Pack\ArrayPack;
use Soen\Array_Package\Pack\IntegerPack;
use Soen\Array_Package\Pack\StringPack;

class PackageProvider
{
    public $package = [
        'string'        =>  String_::class,
        'integer'       =>  LNumber::class,
        'array'         =>  Array_::class
    ];

    public $array = [];

    public function __construct(array $array)
    {
        $this->array = $array;
        $this->stringPack = new StringPack();
        $this->intrgerPack = new IntegerPack();
        $this->arrayPack = new ArrayPack();
        $this->createArrayPack($this->array);
    }



    public function createArrayPack ($propertyArray, &$array_ = null) {
        if (!$array_) {
            $array_ = new ArrayPack();
        }
        foreach ($propertyArray as $key => $value){
            $nodeKey = null;
            $nodeVal = null;
            if(is_string($key)){
                $nodeKey = new StringPack($key);
            }
            if(!is_array($value)){
                if($key === 'class'){
                    $nodeVal = $this->createClassConstFetch($value, 'class');
                } else {
                    $nodeVal = $this->createNodeByType($value);
                }
                $array_->items[$key] = new ArrayItemsPack($nodeVal, $nodeKey);
            }
            if(is_array($value)){
                $array_->items[$key] = new ArrayItemsPack(new ArrayPack(), $nodeKey);
                $this->createArrayPack($value, $array_->items[$key]->value);
            }
        }
        return $array_;
    }

    function createPack($value){
        switch (gettype($value)){
            case 'string':
                return new StringPack($value);
            case 'integer':
                return new IntegerPack($value);
        }
    }

}